package cz.kfad.ofrss.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.View;
import android.widget.*;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import cz.kfad.ofrss.R;
import cz.kfad.ofrss.R.id;
import cz.kfad.ofrss.backend.BackendService;
import cz.kfad.ofrss.backend.IBackend;
import cz.kfad.ofrss.common.ApplicationContext;
import cz.kfad.ofrss.common.DoneCallback;
import cz.kfad.ofrss.common.FeedListAdapter;
import cz.kfad.ofrss.model.RssFeedShort;
import cz.kfad.ofrss.util.CollectionsUtil;

import java.util.List;

public abstract class FeedListActivity extends SherlockListActivity {

    private IBackend backend = BackendService.createBackend(this);
    private ArrayAdapter<RssFeedShort> adapter;
    private DoneCallback done = new DoneCallback() {

        @Override
        public void done() {
            reload();

        }
    };
    private int[] idsToSendToFeedActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_list_with_filter);

        reload();

        EditText filter = (EditText) findViewById(id.feed_list_filter);

        TextWatcher filterWatcher = new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.getFilter().filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }

        };
        filter.addTextChangedListener(filterWatcher);

    }

    public void reload() {
        EditText filter = (EditText) findViewById(id.feed_list_filter);
        filter.setText("");
        final List<RssFeedShort> feeds = getFeedList();
        idsToSendToFeedActivity = CollectionsUtil.createFeedIdsArray(feeds);
        setTitle(getActivityTitle());
        adapter = new FeedListAdapter(this, feeds);
        setListAdapter(adapter);
        registerForContextMenu(getListView());
    }

    @Override
    protected void onResume() {
        super.onResume();
        reload();
        ApplicationContext appState = ((ApplicationContext) getApplicationContext());
        appState.setContextToAsynTasks(this, done);

    }


    protected abstract CharSequence getActivityTitle();

    protected abstract List<RssFeedShort> getFeedList();

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(R.string.feedslist_contextMenu_delete);
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        final CharSequence itemTitle = item.getTitle();
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int itemId = (int) getListAdapter().getItemId(info.position);

        if (getResources().getString(R.string.feedslist_contextMenu_delete).equals(itemTitle)) {
            backend.deleteFeed(itemId);
            Toast.makeText(this, getString(R.string.feedDeleted), Toast.LENGTH_LONG).show();
        } else {
            return false;
        }

        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(R.string.feed_list_menu_refresh).setIcon(android.R.drawable.ic_menu_rotate).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final CharSequence itemTitle = item.getTitle();

        if (getResources().getString(R.string.feed_list_menu_refresh).equals(itemTitle)) {
            ApplicationContext appState = ((ApplicationContext) getApplicationContext());
            appState.executeAsynRefreshTask(this, done);
        } else {
            return false;
        }
        return true;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        final Intent intent = new Intent(this, RssFeedActivity.class);
        intent.putExtra(RssFeedActivity.RSS_ID_KEY, (int) id);
        intent.putExtra(RssFeedActivity.RSS_FEED_IDS_KEY, idsToSendToFeedActivity);
        intent.putExtra(RssFeedActivity.RSS_TITLE_KEY, getActivityTitle());
        startActivity(intent);
    }


}
